
<!--弃用本方案，赋值cookie时页面打不开-->
 <template>
  <div class="login">
    <div class="auth-container">
      <div class="auth-header">
        <h1>授权中</h1>
        <p>即将转跳至应用</p>
      </div>

      <div class="auth-body">
        <div class="info-section" v-loading="loading"></div>
        <div class="security-note">
          安全提示：仅授权您信任的应用。IndSvr不会以任何理由要求您提供密码。
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import {accessToken} from "@/api/system/IndsvrSso";

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      auth: {},
      code: undefined,
      loading: true,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.code = route.query && route.query.code
      },
      immediate: true
    }
  },
  created() {
    this.getAccessToken();
  },
  methods: {

    setCookie(name, value) {
      const date = new Date();
      date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
      const expires = `expires=${date.toUTCString()}`;
      document.cookie = `${name}=${encodeURIComponent(value)}; ${expires}; path=/`;
    },


    getAccessToken() {
      accessToken(this.code).then(res => {
        for (let dataKey in res.data.authList) {
          let datum = res.data.authList[dataKey];

          switch (datum.type) {
            case "localStorage":
              localStorage.setItem(datum.authKey, datum.authValPath);
              break;
            case "sessionStorage":
              sessionStorage.setItem(datum.authKey, datum.authValPath);
              break;
            case "cookies":
              this.setCookie(datum.authKey, datum.authValPath);
              break;
            case "IndexedDB":
              alert("暂不支持DB授权")
              break;
          }
        }


        setTimeout(() => {
          location.href=res.data.redirect;
        }, 10);

      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../../assets/images/login-background.jpg");
  background-size: cover;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.auth-container {
  background-color: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  width: 100%;
  max-width: 500px;
  overflow: hidden;
}

.auth-header {
  background-color: #0052cc;
  color: white;
  padding: 20px;
  text-align: center;
}

.auth-header h1 {
  font-size: 22px;
  margin-bottom: 5px;
}

.auth-header p {
  font-size: 14px;
  opacity: 0.9;
}

.auth-body {
  padding: 30px;
}

.info-section {
  margin-bottom: 25px;
  height: 40px;
}

.info-title {
  font-size: 14px;
  color: #6b7785;
  margin-bottom: 10px;
}

.security-note {
  margin-top: 20px;
  font-size: 13px;
  color: #6b7785;
  text-align: center;
  padding: 15px;
  background-color: #f9fafb;
  border-radius: 8px;
}
</style>
